package com.mindata.blockchain.core.controller;

import com.mindata.blockchain.core.bean.BaseData;
import com.mindata.blockchain.core.bean.ResultGenerator;
import com.mindata.blockchain.core.manager.DbBlockManager;
import com.mindata.blockchain.core.service.TestViewChangeService;
import com.mindata.blockchain.socket.body.base.ViewChangeBody;
import com.mindata.blockchain.socket.common.msg.MsgType;
import com.mindata.blockchain.socket.common.msg.ViewChangeMsg;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags = "区块链接口  换视图测试")
@RestController
@RequestMapping("/viewchange")
public class TestViewChangeController {
    @Resource
    TestViewChangeService viewChangeService;
    @Resource
    DbBlockManager dbBlockManager;
    @Value("${publicKey}")
    private String publicKey;
    @Value("${appId}")
    private String appId;

    @RequestMapping("/change")
    public BaseData viewchange(@RequestBody ViewChangeBody viewChangeBody) {
        return ResultGenerator.genSuccessResult(viewChangeService.handleViewChangeBody(viewChangeBody));
    }

    @RequestMapping("/testchange")
    public String testviewchange() {
        ViewChangeMsg msg = new ViewChangeMsg();
        msg.setVoteType(MsgType.VIEWCHANGE);
        msg.setNumber(dbBlockManager.getLastBlockNumber());
        msg.setHash(dbBlockManager.getLastBlockHash());
        msg.setAppId(appId);
        msg.setAgree(true);

        msg.setToView(dbBlockManager.getLastBlockNumber());
        msg.setPublicKey(publicKey);
        msg.setMsg("viewChange");

        ViewChangeBody viewChangeBody = new ViewChangeBody(msg);

        viewchange(viewChangeBody);
        return "yes";
    }
}
